package com.fairytask.ui.equipUnit
{
	import com.fairytask.engine.GameStage;
	import com.fairytask.engine.components.ToolTips;
	import com.fairytask.engine.components.button.Button;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.objects.Player;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.ui.base.BaseUI;
	import com.fairytask.ui.item.BagItemBak;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	
	/**
	 * 装备部分 
	 * @author xujh
	 * 
	 */
	public class EquipPart extends BaseUI
	{
		public var modelBg		:Clip;	//模型背景
		
		//装备显示
		public var eq_head_bg  :MovieClip;		//头
		public var eq_line_bg  :MovieClip;		//链
		public var eq_arm_bg   :MovieClip;		//武
		public var eq_chest_bg :MovieClip;		//衣
		public var eq_foot_bg  :MovieClip;		//鞋
		public var eq_ring_bg  :MovieClip;		//戒
		
		public var takeoffBtn	:Button;	//卸下按钮
		
		//装备容器
		private var personEquip	:Sprite = new Sprite();
		//人物模型容器
		private var modelSpace		:Sprite	= new Sprite();
		//装备模型容器
		private var weaponSpace	:Sprite	= new Sprite();
		//装备效果
		private var weaponEffect	:Sprite	= new Sprite();
		
		private var _modelMc	:MovieClip;	//人物模型
		private var _weaponMc	:MovieClip;	//装备模型
		private var _effectMode	:MovieClip;	//装备模型
		
		private var _equip		:Array = [];	//装备
		private var _bindView	:Array = [];	//绑定的视图
		
		private var _doubleClick	:Boolean = false;
		private var _curObj	:Point;		//当前对象
		private var _curView	:int;		//当前装备视图
		private var _curPos	:int;		//当前点击对象位置
		private var _btnX		:int;
		private var _btnY		:int;
		private var _canClick	:Boolean = false;	//可以点击
		private var _unbind	:Array = [];
		
		public function EquipPart()
		{
			super();
			initEquip();
			//选项初始化
			takeoffBtn.visible = false;
			takeoffBtn.addEventListener(MouseEvent.CLICK,onTakeOff);
			addChild(takeoffBtn);
		}
		
		/**
		 * 初始化装备
		 */		
		private function initEquip():void
		{
			//把装备放入装备数组，编号从1开始
			_equip=new Array("",eq_head_bg,
				eq_arm_bg,
				eq_foot_bg,
				eq_line_bg,
				eq_chest_bg,
				eq_ring_bg);
			
			personEquip.x = 60;
			personEquip.y = 105;
			addChild(personEquip);
			personEquip.addChild(modelSpace);
			personEquip.addChild(weaponSpace);
			personEquip.addChild(weaponEffect);
			personEquip.mouseEnabled = false;
			personEquip.mouseChildren= false;
			
			//装备面板图标注册监听：单击、双击、拖动……
			for (var i:int=1;i<_equip.length;i++)
			{
				_equip[i].pos = i;
				_equip[i].doubleClickEnabled=true;
				_equip[i].mouseChildren=false;
				_equip[i].addEventListener(MouseEvent.DOUBLE_CLICK,onDouble);
				_equip[i].addEventListener(MouseEvent.CLICK,onClick);
				_equip[i].addEventListener(MouseEvent.MOUSE_OVER,onOver);
				_equip[i].addEventListener(MouseEvent.MOUSE_OUT,onOut);
				var equip:BagItemBak = new BagItemBak();
				equip.x	=	-2;
				equip.y	=	-2;
				_equip[i].addChild(equip);
				_equip[i].data = null;
				_equip[i].equip = equip;
				_equip[i].ObjKey = "";
				GameStage.stage.addEventListener(MouseEvent.MOUSE_UP,hideBtn);
			}
		}
		
		//=======================鼠标事件==========================
		/**
		 * 单击弹出小菜单 
		 * @param evt
		 * 
		 */		
		private function onClick(evt:MouseEvent):void
		{
			if(_canClick)
			{
				//其他模块用信息
				/*if(MainUI.getInstance().checkUI(UIDefine.UniverseUI))
				{
					UniverseUI.getInstance().initProp(evt.currentTarget.data as ViewObject,_curView);
					return;
				}
				else if(MainUI.getInstance().checkUI(UIDefine.RecastUI))
				{
					RecastUI.getInstance().setEquip(evt.currentTarget.data as ViewObject,_curView);
					return;
				}
				else if(MainUI.getInstance().checkUI(UIDefine.RefineUI))
				{
					RefineUI.getInstance().setEquip(evt.currentTarget.data as ViewObject,_curView);
					return;
				}
				else if(MainUI.getInstance().checkUI(UIDefine.PackageUI))
				{
					//显示小菜单
					if(evt.currentTarget.data)
					{
						_doubleClick = false;
						_curPos = (evt.target.data as ViewObject).GetProperties("Pos");
						_btnX = evt.target.x + int(evt.localX);
						_btnY = evt.target.y + int(evt.localY);
						var timer:Timer = new Timer(260,1);
						timer.addEventListener(TimerEvent.TIMER, checkClick);
						timer.start();
					}
				}*/
			}
		}
		
		/**
		 * 选中位置,加选中效果
		 * 
		 */
		private function selectPos(pos:int):void
		{
			
		}
		/**
		 * 双击
		 * @param evt
		 * 
		 */		
		private function onDouble(evt:MouseEvent):void
		{
			_doubleClick = true;
		}
		
		/**
		 * 单击双击判断
		 * @param evt
		 * 
		 */
		private function checkClick(evt:TimerEvent):void 
		{
			if(_doubleClick)
			{
				sendMessage();
			}
			else
			{
				if(!takeoffBtn.visible)
				{
					takeoffBtn.visible = true;
					takeoffBtn.x = _btnX;
					takeoffBtn.y = _btnY;
				}
				else
				{
					takeoffBtn.visible = false;
				}
			}
		}
		
		/**
		 * 点击卸下选项
		 * @param evt
		 * 
		 */		
		private function onTakeOff(evt:MouseEvent):void
		{
			sendMessage();
		}
		
		private function sendMessage():void
		{
			////NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_USEITEM,3,_curView,_curPos,_curObj);
		}
		
		/**
		 * 点击其他地方隐藏菜单
		 * @param evt
		 * 
		 */		
		private function hideBtn(evt:MouseEvent):void
		{
			takeoffBtn.visible = false;
		}
		
		/**
		 * 显示装备tips 
		 * @param evt
		 * 
		 */
		private function onOver(evt:MouseEvent):void
		{
			if(_equip[evt.currentTarget.pos].data!=null)
			{
				/*var viewObj	:ViewObject = model.scene.GetView(_curView).GetProperties(_equip[evt.currentTarget.pos].ObjKey);
				var configID:int=viewObj.GetProperties("ConfigID");
				TipsManage.ShowAllItemsTips(int(StaticIni.getIniVar(String(configID),"ItemType")),configID,viewObj);*/
			}
		}
		
		/**
		 * 隐藏tips 
		 * @param evt
		 * 
		 */
		private function onOut(evt:MouseEvent):void
		{
			ToolTips.hide();
		}
		//=======================绑定数据==========================
		/**
		 * 绑定装备视图 
		 * @param view
		 * 
		 */
		public function bindView(view:int):void
		{
			if(_curView == view)
			{
				return;
			}
			if(_curView != 0)
			{
				clearAll();
			}
			_curView = view;
			//_bindView = [_curView,bindManager.BindView(model.scene, _curView, onAddEquip, onDelEquip)];
		}
		
		/**
		 * 先删除原有装备 
		 * @param key
		 * @param val
		 * @param data
		 * //头部(头)	equip_left.head		= 1
		 //武器(武)	equip_left.arm		= 2,
		 //鞋子(脚)	equip_left.foot		= 3,
		 //项链(项)	equip_right.line	= 4,
		 //铠甲(甲)	equip_left.chest	= 5,
		 //戒指(指)	equip_right.ring	= 6,
		 * 
		 */		
		/*private function onDelEquip(key:String, val:ViewObject, data:Object):void
		{
			var pos	:int	=	val.GetPropertiesEx("Pos");	
			clearEquip(pos);
		}*/
		
		/**
		 * 再添加新装备 
		 * @param key
		 * @param val
		 * @param data
		 * 
		 */		
		/*private function onAddEquip(key:String, val:ViewObject, data:Object):void
		{
			if(val.GetPropertiesEx("ConfigID")=="")
			{
				return;
			}
			var pos:int	=	val.GetPropertiesEx("Pos");
			if(_equip[pos].data == null)
			{
				_unbind[pos] = [val,"ConfigID",bindManager.BindPropInViewObj(model.scene.GetView(_curView),val.ObjKey,"ConfigID",changeImg,null,[pos])];
				_equip[pos].ObjKey = val.ObjKey;
				_equip[pos].data = val;
			}
		}*/
		
		/**
		 * 更改图标 
		 * @param key
		 * @param val
		 * @param pList
		 * 
		 */
		private function changeImg(key:String,val:int,pList:Array):void
		{
			/*var pos	:int = pList[0];
			//物品框颜色设置
			if(null != _equip[pos].frame)
			{
				_equip[pos].removeChild(_equip[pos].frame);
				_equip[pos].frame = null;
				_equip[pos].frameColor = null;
			}
			var frameData:Array	= GoodSet.getFrameData(val);
			var goodFrame:Shape	= frameData[0];
			var frameColor:int	= frameData[1];
			_equip[pos].frame = goodFrame;
			_equip[pos].frameColor = frameColor;
			goodFrame.x = -1;
			goodFrame.y = -1;
			_equip[pos].addChild(goodFrame);
			//物品图标
			_equip[pos].equip.source = ResConfig.iconPath + StaticIni.getIniVar(val,"Icon") + ResConfig.iconImg;*/
		}
		
		/**
		 * 设置人物模型 
		 * @param val
		 * 
		 */
		public function set roleModel(val:String):void
		{
			//SwfLoader.StartLoad(ResConfig.npcResPath+val+"_model.swf",_modelMc,false,onLoadModel);
		}
		
		/**
		 * 设置装备模型 
		 * @param val
		 * 
		 */
		public function set weaponModel(val:int):void
		{
			/*if(val!=0)
			{
				if(Player.getInstance().sex==1)
				{
					SwfLoader.StartLoad(ResConfig.weaponResPath + val + "_1.swf",_weaponMc,false,onWeaponLoad);
				}
				else
				{
					SwfLoader.StartLoad(ResConfig.weaponResPath + val + "_2.swf",_weaponMc,false,onWeaponLoad);	
				}	
			}
			else
			{
				while(weaponSpace.numChildren>0)
				{
					weaponSpace.removeChildAt(0);
				}
			}*/
		}
		
		/**
		 * 设置装备特效 
		 * @param val
		 * 
		 */
		public function set WeaponLevel(val:int):void
		{
			/*if(val!=0)
			{
				if(Player.getInstance().sex==1)
				{
					SwfLoader.StartLoad(ResConfig.weaponResPath + "man" + "_"+val+".swf",_effectMode,false,onWeaponEffectLoaded);
				}
				else
				{
					SwfLoader.StartLoad(ResConfig.weaponResPath + "woman" + "_"+val+".swf",_effectMode,false,onWeaponEffectLoaded);
				}
			}
			else
			{
				while(weaponEffect.numChildren>0)
				{
					weaponEffect.removeChildAt(0);
				}
			}*/
		}
		
		/**
		 * 加载武器特效 
		 * @param val
		 * 
		 */
		private function onWeaponEffectLoaded(val:DisplayObject,pList:Array):void
		{
			if(val)
			{
				while(weaponEffect.numChildren>0)
				{
					weaponEffect.removeChildAt(0);
				}
				_effectMode = val as MovieClip;
				weaponEffect.addChild(_effectMode);
				_effectMode.x = 82;
				_effectMode.y = 76;
				playRes();
			}
		}
		
		/**
		 * 资源同步 
		 * 
		 */
		private function playRes():void
		{
			//同步
			if(null != _weaponMc)
			{
				_weaponMc.gotoAndPlay(0);
			}
			if(null != _modelMc)
			{
				_modelMc.gotoAndPlay(0);
			}
			if(null != _effectMode)
			{
				_effectMode.gotoAndPlay(0);
			}
		}
		
		/**
		 * 加载人物模型 
		 * @param val
		 * @param pList
		 * 
		 */
		private function onLoadModel(val:DisplayObject,pList:Array):void
		{
			_modelMc	=	val as MovieClip;
			if(_modelMc)
			{
				while(modelSpace.numChildren>0)
				{
					modelSpace.removeChildAt(0);
				}
				_modelMc.x	=	82;
				_modelMc.y	=	76;
				modelSpace.addChild(_modelMc);
				playRes();
			}
		}
		
		/**
		 * 加载装备模型 
		 * @param val
		 * @param pList
		 * 
		 */
		private function onWeaponLoad(val:DisplayObject,pList:Array):void
		{
			if(val)
			{
				while(weaponSpace.numChildren>0)
				{
					weaponSpace.removeChildAt(0);
				}
				_weaponMc = val as MovieClip;
				weaponSpace.addChild(_weaponMc);
				_weaponMc.x = 82;
				_weaponMc.y = 76;
				//同步
				_weaponMc.mouseEnabled = false;
				_weaponMc.mouseChildren= false;
				playRes();
			}
		}
		
		/**
		 * 当前视图id 
		 * @return 
		 * 
		 */
		public function get curViewId():int
		{
			return _curView;
		}
		//==========================清除方法=======================================
		/**
		 * 清除所有内容，为加载新的内容做准备 
		 * 
		 */
		public function clearAll():void
		{
			//清除人物模型
			while(modelSpace.numChildren>0)
			{
				modelSpace.removeChildAt(0);
			}
			//清除装备模型
			while(weaponSpace.numChildren>0)
			{
				weaponSpace.removeChildAt(0);
			}
			//清除装备特效
			while(weaponEffect.numChildren>0)
			{
				weaponEffect.removeChildAt(0);
			}
			//清除装备显示
			for(var i:int=1;i<_equip.length;i++)
			{
				clearEquip(i);
			}
			//解绑数据 
			removeBindData();
		}
		
		/**
		 * 清除对应位置装备
		 * @param pos
		 * 
		 */
		private function clearEquip(pos:int):void
		{
			if(_equip[pos].data!=null)
			{
				//				(_equip[pos]as MovieClip).removeChild(_equip[pos].equip);
				(_equip[pos] as MovieClip).removeChild(_equip[pos].frame);
				_equip[pos].equip.clearPhoto();
				_equip[pos].data=null;
				_equip[pos].ObjKey = "";
				//				_equip[pos].clip=null;
				_equip[pos].frame=null;
				
				_equip[pos].frameColor=null;
			}
		}
		
		/**
		 * 解绑数据 
		 * 
		 */
		private function removeBindData():void
		{
			/*if(_bindView.length>0)
			{
				bindManager.UnbindView(_bindView.shift(),_bindView.shift());
				_curView = 0;
			}*/
		}
		
		/**
		 * 设置装备是否可以点击 
		 * @param val
		 * 
		 */
		public function set canClick(val:Boolean):void
		{
			_canClick = val;
		}
		/**
		 *其他玩家装备模型 
		 * @param val
		 * @param sex
		 * 
		 */
		public function oppWeaponModel(val:int,sex:int):void
		{
			if(val!=0)
			{
				if(sex==2)
				{
					//SwfLoader.StartLoad(ResConfig.weaponResPath + val + "_2.swf",_weaponMc,false,onWeaponLoad);
				}
				else if(sex==1)
				{
					//SwfLoader.StartLoad(ResConfig.weaponResPath + val + "_1.swf",_weaponMc,false,onWeaponLoad);	
				}
				else
				{
					weaponModel = 0;
				}
			}
			else
			{
				while(weaponSpace.numChildren>0)
				{
					weaponSpace.removeChildAt(0);
				}
			}
		}
	}
}